# -*- coding: utf8 -*-
# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages

import wx                        # This module uses the new wx namespace
import images
import AboutNotebook
import MementoProvider

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

#---------------------------------------------------------------------------

class My_TaskBarIcon(wx.TaskBarIcon):
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)
        
        #-------------------------------------------------------------------
        
        self.frame = frame
        
        #-------------------------------------------------------------------

        bmp = images.Icon_App.GetIcon()
        icon = self.MakeIcon(images.Icon_App.GetImage())
        #icon = self.MakeIcon(bmp)
        self.SetIcon(icon, tooltip=_("PyArchiver"))
        
        #-------------------------------------------------------------------
        
        # Bind the menu event to an events handler
        self.Bind(wx.EVT_MENU, self.frame.OnAbout, id=1)
        self.Bind(wx.EVT_MENU, self.frame.OnMemento, id=2)
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=3)
        self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=4)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=5)
        # self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarClose)
        
    #-----------------------------------------------------------------------
        
    def CreatePopupMenu(self):
        menu = wx.Menu(style=wx.MENU_TEAROFF)
        
        #-------------------------------------------------------------------

        bmp = images.Item_About.GetBitmap()
        
        item = wx.MenuItem(menu, 1, text=_("About..."))
        item.SetBitmap(bmp)
        menu.AppendItem(item)
        menu.AppendSeparator()
        
        #-------------------------------------------------------------------

        bmp = images.Item_Note.GetBitmap()
                        
        item = wx.MenuItem(menu, 2, text=_("Memento"))
        item.SetBitmap(bmp)
        menu.AppendItem(item)
        menu.AppendSeparator()
        
        #-------------------------------------------------------------------

        bmp = images.Item_Empty.GetBitmap()
                        
        item = wx.MenuItem(menu, 3, text=_("Show PyArchiver"))
        item.SetBitmap(bmp)
        menu.AppendItem(item)
        
        #-------------------------------------------------------------------

        bmp = images.Item_Empty.GetBitmap()
                        
        item = wx.MenuItem(menu, 4, text=_("Hide PyArchiver"))
        item.SetBitmap(bmp)
        menu.AppendItem(item)
        menu.AppendSeparator()
        
        #-------------------------------------------------------------------

        bmp = images.Item_Exit.GetBitmap()
                        
        if True or "wxMSW" in wx.PlatformInfo:
            font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
            font.SetWeight(wx.BOLD)
        
        item = wx.MenuItem(menu, 5, text=_("Exit PyArchiver"))
        item.SetBitmap(bmp)
        item.SetFont(font)
        menu.AppendItem(item)        
        return menu

    
    def MakeIcon(self, img):
        """
        The various platforms have different requirements
        for the icon size...
        """
     
        if "wxMSW" in wx.PlatformInfo:
            img = img.Scale(16, 16)
        elif "wxGTK" in wx.PlatformInfo:
            img = img.Scale(22, 22)
        # wxMac can be any size upto 128x128, so leave the source img alone....
        icon = wx.IconFromBitmap(img.ConvertToBitmap() )
        return icon
     

    def OnTaskBarActivate(self, event):
        if not self.frame.IsShown():
            self.frame.Show()


    def OnTaskBarDeactivate(self, event):
        if self.frame.IsShown():
            self.frame.Hide()
            

    def OnTaskBarClose(self, event):
        wx.CallAfter(self.frame.Close)


